home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / teco.zip / WORK.C < prev   
C/C++ Source or Header  |  1986-07-15  |  12KB  |  373 lines

  1. #include <ctype.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. work()
  6. {
  7. #include "teco.h"
  8.  
  9.     int iter;                /* Iteration  < > count */
  10.     int prefix;                /* Non-zero if prefix   */
  11.     int sign;                /* Sign  of number      */
  12.     int tmp;                /* Scratch variable     */
  13.     int value;                /* Value of number      */
  14.     int wrk;                /* Another scratch var. */
  15.  
  16.     append();                /* Read in first buffer */
  17.  
  18.     bufptx=0;                /* Start at head of bfr */
  19. loop:    cancel=0;                /* Show output  allowed */
  20.     comand();                /* Get the comand strng */
  21.     getptx=0;                /* Start of command buf */
  22.     iter=0;                    /* Not any <> iteration */
  23. next:    if (++getptx > getptr) {        /* Any more commands?   */
  24.         if (iter) fprintf(stderr,"?UTC, Unterminated command\n\7");
  25.         goto loop;            /*  ..yes then go again */
  26.     }
  27.     if (getbuf[getptx] == 27) goto next;    /* Punt Delimiter */
  28.     prefix=0;                /* No numeric arg */
  29.     sign=1;                 /* Default  sign  */
  30.     value=1;                /* Default  value */
  31.     if (getbuf[getptx] == '+') {
  32.         sign=1;                /* Positive sign  */
  33.         getptx++;
  34.     } else {
  35.         if (getbuf[getptx] == '-') {
  36.             sign=-1;        /* Negative sign  */
  37.             getptx++;
  38.         }
  39.     }
  40.     tmp=getptx;                /* Start  parse   */
  41.     if (isdigit(getbuf[tmp])) {        /*  ...is number  */
  42.         prefix=1;            /*  ...not deflt  */
  43.         value=0;            /*  ...initialize */
  44.         while (isdigit(getbuf[tmp])) {
  45.             value=value*10+(getbuf[tmp++]-'0');
  46.             }
  47.     } else {
  48.         if (getbuf[tmp] == '.'){    /* Dot means THIS */
  49.             prefix=1;        /*  ....not deflt */
  50.             value=bufptx;        /* Value is  THIS */
  51.             tmp++;            /* Accept it      */
  52.         }
  53.         if (toupper(getbuf[tmp]) == 'B'){
  54.             prefix=1;        /*  ....not deflt */
  55.             value=0;        /* Always is zero */
  56.             tmp++;            /* Accept it      */
  57.         }
  58.         if (toupper(getbuf[tmp]) == 'Z'){
  59.             prefix=1;        /*  ....not deflt */
  60.             value=bufptr;        /* Value is   END */
  61.             tmp++;            /* Accept it      */
  62.         }
  63.     }
  64.     number=sign*value;            /* Specific value */
  65.     getptx=tmp;                /* getptx --> Cmd */
  66.  
  67.     verb=toupper(getbuf[getptx]);        /* VERB is action */
  68.     if (verb == 27 ) goto next;        /* Missing  verb  */
  69.     if (verb == '=') {            /* Show our globl */
  70.         if (prefix) {
  71.             fprintf(stderr,"%d\n",number);
  72.         } else {
  73.             fprintf(stderr,"?NAE, No arg before =\n\7");
  74.         }
  75.         goto next;            /* punt, all done */
  76.     } else {
  77.           adverb=toupper(getbuf[getptx+1]);    /* verb qualifier */
  78.     }                    /*   ...is adverb */
  79.  
  80.     if (verb == '>') {            /* End iteration */
  81.         if (!iter)  {            /*  ..must exist */
  82.             fprintf(stderr,"?BNI, Not in iteration\n\7");
  83.             goto loop;        /*  ...flush buf */
  84.         }
  85.         if (--iter) {            /*  ..find start */
  86.             while (getbuf[--getptx] != '<');
  87.         }                /*  ..must exist */
  88.         goto next;            /*  ...continue  */
  89.     }
  90.     if (verb == '<') {            /* Iteration mode */
  91.         if (iter)  {            /*  ...norecursiv */
  92.             fprintf(stderr,"?PDO, Push-down list overflow\n\7");
  93.             goto loop;
  94.         }
  95.         if (!prefix) {            /*  ...no limits  */
  96.             iter=32766;        /*  ...big enuff  */
  97.         } else {            /*  ...use his v  */
  98.             iter=value;        /*  ...supplied   */
  99.         }                /*  ...finished   */
  100.         goto next;            /*  ...have more  */
  101.     }
  102.     if (verb == 'A') {            /* Append a  page */
  103.         while (0 < number--) append();    /*  ...with call  */
  104.         goto next;            /*  ...punt this  */
  105.     }
  106.  
  107.     if (verb == 'C') {            /* Advance char   */
  108.         bufptx=bufptx+number;        /*  on  request   */
  109.         if (bufptx<0) {            /* Insane request */
  110.             fprintf(stderr,"?POP, pointer off page\n\7");
  111.             bufptx=0;        /* Minimum place  */
  112.             goto loop;        /*  ...abort this */
  113.             }
  114.         if (bufptx>bufptr) {        /* More  insanity */
  115.             fprintf(stderr,"?POP, pointer off page\n\7");
  116.             bufptx=bufptr;        /* Maximum place  */
  117.             goto loop;        /*  ...abort this */
  118.         }
  119.         goto next;            /* Eat some more  */
  120.     }
  121.  
  122.     if (verb == 'D') {            /* Delete char    */
  123.         if (number == 0) goto next;    /* Must exist     */
  124.         if (number <  0) {        /* Delete backwds */
  125.             bufptx=bufptx+number;    /*  ...back up    */
  126.             number=abs(number);    /*  ...magnitude  */
  127.         }                /*  ...delete for */
  128.         if (bufptx < 0) {        /* Sanity chk #1  */
  129.             fprintf(stderr,"?DTB, Delete too big\n\7");
  130.             goto loop;        /*  ...flush all  */
  131.         }                /*  ...of buffer  */
  132.         if (bufptx+number > bufptr) {    /* Sanity chk #2  */
  133.             fprintf(stderr,"?DTB, Delete too big\n\7");
  134.             goto loop;        /*  ...flush all  */
  135.         }                /*  ...of buffer  */
  136.         memcpy(&buffer[bufptx+1],&buffer[bufptx+number+1],bufptr-bufptx);
  137.         bufptr=bufptr-number;        /* Show deleted.. */
  138.         if (bufptr < bufptx) {        /*  ..not past nd */
  139.             bufptx=bufptr;        /* Impose  sanity */
  140.         }                /*  ..stop  this  */
  141.         goto next;            /*  ...punt this  */
  142.     }
  143.     if (verb == 'E' ) {            /* Buffer  exits  */
  144.         getptx++;            /* Advance pointr */
  145.         if (adverb == 'F') return;    /* Terminate file */
  146.         if (adverb == 'X') {        /* Orderly   exit */
  147.             while (bufptr) page();    /* Write out page */
  148.             return;            /*  ...and leave  */
  149.         }
  150.           fprintf(stderr,"?IEC, Illegal character '%c' after E\n\7",adverb);    
  151.         goto loop;            /* Punt commands  */
  152.     }
  153.     if (verb == 'F' ) {            /* Replaqe string */
  154.         getptx++;            /* Advance pointr */
  155.         if (adverb=='S' | adverb=='N') {/* ...replace cmd */
  156.             if (1 > number) {
  157.                 fprintf(stderr,"?ISA, Illegal search arg\n\7");
  158.                 goto loop;
  159.             }
  160.             if (getbuf[++getptx] == 27) goto next;
  161.  
  162.             while (number--) {    /* Scan from here */
  163.                 if (adverb == 'S'){/* Local in scope */
  164.                     search();
  165.                 } else {    /* Global replace */
  166.                     next();
  167.                 }
  168.                 if (!bufptx) {    /*  ...not  found */
  169.                       tmp=getptx;
  170.                       while (getbuf[++getptx] != 27);
  171.                       getbuf[getptx]='\0';
  172.                       fprintf(stderr,"?SRH, Search failure ");
  173.                       fprintf(stderr,"'%s'\n\7",&getbuf[tmp]);
  174.                       goto loop;
  175.                 }
  176.              tmp=0;
  177.              while (getbuf[getptx + ++tmp] != 27);
  178.              memcpy(&buffer[bufptx],&buffer[bufptx+tmp],bufptr-bufptx);
  179.              bufptr=bufptr-tmp;
  180.  
  181.              wrk=0;
  182.              while (getbuf[getptx + tmp + ++wrk] != 27);
  183.              wrk--;
  184.              bufptr++;
  185.              if (bufptr+wrk > bufsiz) {
  186.             fprintf(stderr,"?MEM, Memory overflow\n\7");
  187.             goto loop;
  188.              }
  189.              if (bufptr > bufptx) {
  190.               memcpy(&buffer[bufptx+wrk],&buffer[bufptx],bufptr-bufptx);
  191.              }
  192.              if (wrk > 0) {
  193.             memcpy(&buffer[bufptx],&getbuf[getptx+tmp+1],wrk);
  194.              }
  195.              bufptx--;
  196.              bufptr--;
  197.              bufptr=bufptr+wrk;
  198.              bufptx=bufptx+wrk;
  199.              }
  200.              while (getbuf[++getptx] != 27);
  201.              while (getbuf[++getptx] != 27);
  202.              goto next;
  203.         }
  204.     fprintf(stderr,"?ILL, Illegal command '%c%c'\n\7",verb,adverb);
  205.     goto loop;
  206.     }
  207.     if (verb == 'H' ) {            /* Hole   thingy  */
  208.         getptx++;            /* Advance pointr */
  209.         if (adverb == 'T') {        /* Type the buffr */
  210.             tmp=0;            /* Start at begin */
  211.             while (++tmp<=bufptr ){    /*  ...start list */
  212.                 echo(buffer[tmp]);
  213.             }            /*  ...all done   */
  214.             goto next;        /*  ...fetch next */
  215.         }
  216.         if (adverb == 'K') {        /* Kill the bufer */
  217.             bufptr=0;        /* Nothing in it  */
  218.             bufptx=0;        /* Force at end   */
  219.             goto next;        /* Fetch next com */
  220.         }
  221.         fprintf(stderr,"?ILL, Illegal command '%c%c'\n\7",verb,adverb);
  222.         goto loop;            /* Punt the error */
  223.     }
  224.  
  225.     if (verb == 'I' | verb == 9) {        /* Insert    text */
  226.         if (verb == 'I') ++getptx;    /* Skip 'I'  only */
  227.         tmp=getptx;            /* Grab a pointer */
  228.         if (prefix) {            /* Character inst */
  229.             if (getbuf[tmp] != 27) {
  230.                 fprintf(stderr,"?IIA, Illegal insert arg\n\7");
  231.                 goto loop;
  232.             }
  233.             bufptr++;
  234.             bufptx++;
  235.             if (bufptr > bufsiz) {
  236.                 fprintf(stderr,"?MEM, Memory overflow\n\7");
  237.                 goto loop;
  238.             }
  239.             if (bufptr > bufptx) {    /* Sanity  check  */
  240.             memcpy(&buffer[bufptx+1],&buffer[bufptx],bufptr-bufptx);
  241.             }            /* ..only if sane */
  242.             buffer[bufptx]=toascii(number);
  243.             goto next;        /* Eat some  more */
  244.         }
  245.         while (getbuf[++tmp] != 27);    /* Find string nd */
  246.         bufptr++;
  247.         bufptx++;
  248.         if (bufptr+tmp > bufsiz) {
  249.             fprintf(stderr,"?MEM, Memory overflow\n\7");
  250.             goto loop;
  251.         }
  252.         if (bufptr > bufptx) {        /* Sanity  check  */
  253.         memcpy(&buffer[bufptx+tmp-getptx],&buffer[bufptx],bufptr-bufptx);
  254.         }                /* ..only if sane */
  255.         memcpy(&buffer[bufptx],&getbuf[getptx],tmp-getptx);
  256.         bufptx--;            /* Undo the fudge */
  257.         bufptr--;            /*  ...also fudge */
  258.         bufptr=bufptr+tmp-getptx;    /* New buffer siz */
  259.         bufptx=bufptx+t